package com.newcapec.modules.pass.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.newcapec.common.utils.PageUtils;
import com.newcapec.common.utils.R;
import com.newcapec.modules.pass.entity.TemporaryAccessEntity;
import com.newcapec.modules.pass.service.TemporaryAccessService;
import com.newcapec.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 临时出入申请表
 *
 * @author steven
 * @email wangsen127@163.com
 * @date 2022-01-10 09:29:04
 */
@RestController
@RequestMapping("pass/temporaryaccess")
public class TemporaryAccessController extends AbstractController {
    @Autowired
    private TemporaryAccessService temporaryAccessService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @RequiresPermissions("pass:temporaryaccess:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = temporaryAccessService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 列表全部
     */
    @GetMapping("/listAll")
    @RequiresPermissions("pass:temporaryaccess:list")
    public R listAll(@RequestParam Map<String, Object> params){
        Object status = params.get("status");
        Object studentId = params.get("studentId");
        QueryWrapper<TemporaryAccessEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(status!=null, "status", status);
        queryWrapper.eq(studentId!=null, "student_id", studentId);
        queryWrapper.orderByDesc("create_time");
        List<TemporaryAccessEntity> list = temporaryAccessService.list(queryWrapper);

        return R.ok().put("list", list);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @RequiresPermissions("pass:temporaryaccess:info")
    public R info(@PathVariable("id") Long id){
		TemporaryAccessEntity temporaryAccess = temporaryAccessService.getById(id);

        return R.ok().put("temporaryAccess", temporaryAccess);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @RequiresPermissions("pass:temporaryaccess:save")
    public R save(@RequestBody TemporaryAccessEntity temporaryAccess){
		temporaryAccessService.save(temporaryAccess);

        return R.ok();
    }

    /**
     * 修改：废弃
     */
    @Deprecated
    @PutMapping("/update")
    @RequiresPermissions("pass:temporaryaccess:update")
    public R update(@RequestBody TemporaryAccessEntity temporaryAccess){
		temporaryAccessService.updateById(temporaryAccess);

        return R.ok();
    }

    /**
     * 批量修改
     */
    @PutMapping("/updateBatch")
    @RequiresPermissions("pass:temporaryaccess:update")
    public R updateBatch(@RequestBody List<TemporaryAccessEntity> temporaryAccessList){
        temporaryAccessList.forEach(item -> {
            item.setApproveTime(new Date());
            item.setApproveUser(getUser().getUsername());
        });
        temporaryAccessService.updateBatchById(temporaryAccessList);

        return R.ok();
    }

    /**
     * 删除：废弃
     */
    @Deprecated
    @DeleteMapping("/delete")
    @RequiresPermissions("pass:temporaryaccess:delete")
    public R delete(Long[] ids){
		temporaryAccessService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }
}
